// pages/movie/movie.js
Page({
  data: {

  },

  onLoad: async function (options) {
    var module = await import("../../utils/util");
    var bannerRes = await module.mvHttp.getBanner();
    var top250Res = await module.mvHttp.getTop250();
    var comingSoonRes = await module.mvHttp.getComingSoon();
    var inTheatersRes = await module.mvHttp.getInTheaters();
    this.handleStar(top250Res);
    this.handleStar(comingSoonRes);
    this.handleStar1(inTheatersRes);
    this.setData({
      banners: bannerRes.data.res,
      top250: top250Res.data.res.slice(0, 3),
      comingSoon: comingSoonRes.data.res.slice(0, 3),
      inTheaters: inTheatersRes.data.res.slice(0, 3)
    })
  },
  handleStar(res) {
    res.data.res.forEach(item=>{
      var num = item.rating;
      var star = [];
      for(var i = 0 ; i < 5 ; i++){
        if(num>=2){
          star.push(2);
        }else if(num>0){
          star.push(parseFloat(num.toFixed(1)));
        }else{
          star.push(0);
        }
        num -= 2;
      }
      item.star = star;
    })
    return res;
  },
  handleStar1(res) {
    res.data.res.forEach(item=>{
      var num = item.raiting;
      var star = [];
      for(var i = 0 ; i < 5 ; i++){
        if(num>=2){
          star.push(2);
        }else if(num>0){
          star.push(parseFloat(num.toFixed(1)));
        }else{
          star.push(0);
        }
        num -= 2;
      }
      item.star = star;
    })
    return res;
  }
})